package com.zx._01_基础._013_io;

import com.zx.common.Constant;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import junit.framework.TestCase;
import lombok.Cleanup;

/**
 * 使用缓存操作io流
 */
public class Demo_05_Buffer extends TestCase {

    @SuppressWarnings("resource")
    public void test1() throws IOException {
        //1.读取这个文件
        @Cleanup FileInputStream fi = new FileInputStream(Constant.io_read_text);
        //2.将这个字节流转化为字符流
        @Cleanup InputStreamReader isr = new InputStreamReader(fi, "UTF-8");
        //3.将这个字符流经行buffer包装
        @Cleanup BufferedReader br = new BufferedReader(isr);

        String s = null;

        while ((s = br.readLine()) != null) {
            //读取每一行
            System.out.println(s);
        }

    }

    public void test2() throws IOException {
        //1.读取这个文件
        Reader r = new FileReader("src/io_read.txt");

        //2.将这个字符流经行buffer包装
        BufferedReader br = new BufferedReader(r);

        String s = null;
        while ((s = br.readLine()) != null) {
            System.out.println(s);
        }
    }

}
